//package com.example.springai.http;
//
//import com.example.springai.http.pojo.v1.ChatRequest;
//import com.example.springai.http.pojo.v1.ChatRequestExample;
//import org.springframework.ai.chat.messages.UserMessage;
//import org.springframework.ai.chat.prompt.Prompt;
//import org.springframework.ai.model.function.FunctionCallback;
//import org.springframework.ai.openai.OpenAiChatModel;
//import org.springframework.ai.openai.OpenAiChatOptions;
//import org.springframework.ai.openai.api.OpenAiApi;
//import org.springframework.ai.openai.api.ResponseFormat;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.ResponseEntity;
//import org.springframework.web.bind.annotation.*;
//
//import java.lang.reflect.InvocationTargetException;
//import java.lang.reflect.Method;
//import java.util.*;
//
//@RequestMapping("/deepseek")
//@RestController
//@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST})
//public class DeepSeekController {
//    @Autowired
//    private OpenAiChatModel deepseekChatModel;
//
//
//    @GetMapping(value = "/openai/functionCalling/v1")
//    public Map<String, String> functionCallingv1(@RequestParam(value = "message", defaultValue = "北大有多少个专业?") String message) {
//        UserMessage userMessage = new UserMessage(message);
//        final String[] body1 = new String[1];
//        Map<String, String> bodyMap = new HashMap<>();
//        body1[0] = "系统出现BUG，请稍后再试";
//        Method method = null;
//        ChatRequest.Message system = new ChatRequest.Message("system", "你是公司内部的AI助手，请格式化输出内容到html的<body>标签中");
//        ChatRequest.Message user = new ChatRequest.Message("user", message);
//        try {
//            method = OpenAiChatModel.class.getDeclaredMethod("createRequest", Prompt.class, boolean.class);
//            method.setAccessible(true);
//            OpenAiApi.ChatCompletionRequest currentWeather = (OpenAiApi.ChatCompletionRequest) method.invoke(deepseekChatModel, new Prompt(userMessage,
//                    OpenAiChatOptions.builder().responseFormat(ResponseFormat.builder()
//                                    .type(ResponseFormat.Type.TEXT)
//                                    .build())
//                            .functions(Set.of("queryMajorCount", "queryCollegeCount", "queryHotSchool", "addSchool"))// Enable the function
//                            .build()), false);
//            List<OpenAiApi.FunctionTool> tools = currentWeather.tools();
//            ResponseEntity<OpenAiApi.ChatCompletion> chatCompletionChunkResponseEntity = ChatRequestExample.requestDeepSeekChat(tools, List.of(system, user));
//            Map<String, FunctionCallback> functionCallbackRegister = deepseekChatModel.getFunctionCallbackRegister();
//            OpenAiApi.ChatCompletion body = chatCompletionChunkResponseEntity.getBody();
//            if (body != null) {
//                {
//                    List<OpenAiApi.ChatCompletionMessage.ToolCall> toolCalls = body.choices().get(0).message().toolCalls();
//                    if (toolCalls == null || toolCalls.isEmpty()) {
//                        body1[0] = body.choices().get(0).message().content();
//                        ;
//                        bodyMap.put("content", body1[0]);
//                        return bodyMap;
//                    }
//                    body.choices().forEach(choice -> {
//                        //TODO 管理添加的功能，需要人工二次确认才可行
//                        Iterator<OpenAiApi.ChatCompletionMessage.ToolCall> iterator = choice.message().toolCalls().iterator();
//                        while (iterator.hasNext()) {
//                            OpenAiApi.ChatCompletionMessage.ToolCall toolCall = iterator.next(); // 获取当前元素
//                            FunctionCallback functionCallback = functionCallbackRegister.get(toolCall.function().name());
//                            String call = functionCallback.call(toolCall.function().arguments());
//                            // 如果不是最后一个元素，继续迭代
//                            if (iterator.hasNext()) {
//
//                            } else {
//                                // 如果是最后一个元素，执行额外的逻辑
//                                ResponseEntity<OpenAiApi.ChatCompletion>
//                                        chatCompletionChunkResponseEntity1 = ChatRequestExample.requestDeepSeekChat(null, List.of(
//                                        system,
//                                        user,
//                                        new ChatRequest.Message("user", call)
//                                ));
//                                body1[0] = chatCompletionChunkResponseEntity1.getBody().choices().get(0).message().content();
//                                bodyMap.put("content", body1[0]);
//                            }
//                        }
//                    });
//                }
//            }
//            return bodyMap;
//        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
//            throw new RuntimeException(e);
//        }
//
//    }
//
//}
